const localData = localStorage.getItem('pz_v3pz')

const state = localData ? localData.menu : {
    // 侧边栏状态
    isCollapse: false,
    // 侧边栏选中的菜单
    selectMenu: [],
    routerList: [],
    menuActive: '1-1'
}
// 用于记录当前数据
const mutations = {
    // 切换侧边栏状态数据
    collapseMenu(state) {
        state.isCollapse = !state.isCollapse
    },
    //playload为传入的菜单数据
    addMenu(state, payload) {
        //对数据进行去重
        if (state.selectMenu.findIndex(item => item.path === payload.path) === -1) {
            state.selectMenu.push(payload)
        }
    },
    closeMenu(state, payload) {
        //获取点击数据的索引
        const index = state.selectMenu.findIndex(val => val.name === payload.name)
        //根据索引删除数据
        state.selectMenu.splice(index, 1)
    },
    dynamicMenu(state, payload) {

        //通过glob导入文件
        const modules = import.meta.glob('../views/**/**/*.vue')
        console.log(modules);
        function routerSet(router) {
            router.forEach(route => {
                //判断没有子菜单，拼接路由数据
                if (!route.children) {
                    const url = `../views${route.meta.path}/index.vue`
                    //拿到获取的vue组件
                    route.component = modules[url]
                } else {
                    routerSet(route.children)
                }
            })
        }
        routerSet(payload)
        state.routerList = payload
    },
    updateMenuActive(state, payload) {
        state.menuActive = payload
    }
}
export default {
    state,
    mutations
}